package com.wolfcode.goods.service.impl;

import com.wolfcode.goods.domain.GoodsCategory;
import com.wolfcode.goods.domain.info.BusiPathInfo;
import com.wolfcode.goods.domain.info.GoodsAndGoodsCategoryInfo;
import com.wolfcode.goods.domain.vo.GoodsCategoryVo;
import com.wolfcode.goods.mapper.GoodsCategoryMapper;
import com.wolfcode.goods.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 物品分类信息Service业务层处理
 * 
 * @author sitia
 * @date 2022-12-10
 */
@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService 
{
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询物品分类信息
     * 
     * @param id 物品分类信息主键
     * @return 物品分类信息
     */
    @Override
    public GoodsCategory selectGoodsCategoryById(Integer id)
    {
        return goodsCategoryMapper.selectGoodsCategoryById(id);
    }

    /**
     * 查询物品分类信息列表
     * 
     * @param goodsCategory 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategoryVo goodsCategory)
    {
        List<String> busiPaths = goodsCategory.getBusiPath();
        GoodsCategory select = new GoodsCategory();
        if (busiPaths != null) {
            String busiPath = busiPaths.get(busiPaths.size() - 1);
            select.setBusiPath(busiPath);
        }
        select.setCategoryName(goodsCategory.getCategoryName());

        return goodsCategoryMapper.selectGoodsCategoryList(select);
    }

    /**
     * 新增物品分类信息
     * 
     * @param goodsCategoryVo 物品分类信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoodsCategory(GoodsCategoryVo goodsCategoryVo)
    {
        Assert.notNull(goodsCategoryVo,"方法请求");
        Assert.notNull(goodsCategoryVo.getCategoryName(),"分类名称不能为空");
        List<String> busiPaths = goodsCategoryVo.getBusiPath();
        GoodsCategory goodsCategory = new GoodsCategory();
        String busiPath = null;
        if (busiPaths != null) {
            busiPath = busiPaths.get(busiPaths.size() - 1);//前端传入的路径
            Long partentId = goodsCategoryMapper.getIdByBusiPath(busiPath);
            goodsCategory.setParentId(partentId);
        }
        goodsCategory.setCategoryName(goodsCategoryVo.getCategoryName());
        goodsCategory.setCategoryDesc(goodsCategoryVo.getCategoryDesc());
        goodsCategoryMapper.insertGoodsCategory(goodsCategory);
        //判断是否有父类，没有id就是路径,有则需要拼接父类的路径
        GoodsCategory updateGoodsCategory = new GoodsCategory();
        //插入数据后新数据的id
        Integer id = goodsCategory.getId();
        String newBusiPath = null;
        updateGoodsCategory.setId(id);
        if (goodsCategory.getParentId() != null) {
            newBusiPath = busiPath + ":" + id.toString();
            updateGoodsCategory.setBusiPath(newBusiPath);
        } else {
            updateGoodsCategory.setBusiPath(id.toString());
        }
        return goodsCategoryMapper.updateGoodsCategory(updateGoodsCategory);
    }

    /**
     * 修改物品分类信息
     * 
     * @param goodsCategory 物品分类信息
     * @return 结果
     */
    @Override
    public int updateGoodsCategory(GoodsCategory goodsCategory)
    {
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    /**
     * 批量删除物品分类信息
     * 
     * @param id 需要删除的物品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryByIds(Integer id) throws Exception {
        GoodsCategory goodsCategory = goodsCategoryMapper.selectGoodsCategoryById(id);
        List<GoodsAndGoodsCategoryInfo> info = goodsCategoryMapper.getGoodsAndGoodsCategoryInfoByGoodsCategoryPath(goodsCategory.getBusiPath());
        if (info.size() != 0) {
            throw new Exception("与物品信息存在关联，不能删除");
        }
        List<GoodsCategory> siongoodsCategorys = goodsCategoryMapper.getSiongoodsCategory(goodsCategory.getBusiPath());
        List<Integer> ids = new ArrayList<>();
        for (GoodsCategory siongoodsCategory : siongoodsCategorys) {
            ids.add(siongoodsCategory.getId());
        }
        Integer[] idss = ids.toArray(new Integer[0]);

        return goodsCategoryMapper.deleteGoodsCategoryByIds(idss);
    }

    /**
     * 删除物品分类信息信息
     * 
     * @param id 物品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsCategoryById(Integer id)
    {
        return goodsCategoryMapper.deleteGoodsCategoryById(id);
    }

    public List<BusiPathInfo> findSon(List<BusiPathInfo> busiPathInfos) {
        for (BusiPathInfo busiPathInfo: busiPathInfos) {
            List<BusiPathInfo> busiPath = goodsCategoryMapper.getBusiPathInfoByBusiPath(busiPathInfo.getId());
            if (busiPath != null) {
                busiPathInfo.setChildren(busiPath);
                findSon(busiPath);
            }
        }
        return busiPathInfos;
    }
    @Override
    public List<BusiPathInfo> getBusiPath() {
        //List<BusiPathInfo> busiPathInfo = goodsCategoryMapper.getBusiPathInfo();
        List<BusiPathInfo> parentBusiPath = goodsCategoryMapper.getParentBusiPath();
        List<BusiPathInfo> busiPathInfos = findSon(parentBusiPath);
        System.out.println(busiPathInfos);
        return busiPathInfos;
    }
}
